package com.sharemarking.wa.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.Org;
import com.sharemarking.wa.common.entity.OrgCar;
import com.sharemarking.wa.common.entity.OrgMember;
import com.sharemarking.wa.common.exception.DriverException;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.carManagement.CarListDto;
import com.sharemarking.wa.platform.dto.cms.CmsChannelListDto;
import com.sharemarking.wa.platform.dto.cms.CmsPostInfoDto;
import com.sharemarking.wa.platform.dto.cms.CmsPostListDto;
import com.sharemarking.wa.platform.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sharemarking.wa.common.SysHttpStatus.UN_DRIVER_VERIFY;

/**
 * @author dxy
 */
@Service
public class CarManagementService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private CarManagementMapper carManagementMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrgCarMapper orgCarMapper;
    @Autowired
    private OrgMemberMapper orgMemberMapper;


    public ResponseParams<?> getList() throws Exception {
        Integer memberId = ss.getCurrentUserId();

        Integer role = memberMapper.getRoleByMemberId(memberId);
        if (role == null || !role.equals(3)) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        List<CarListDto> list = carManagementMapper.getList(params);
        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, carManagementMapper.getListTotal(params), list.size() == pageSize));
    }


    public ResponseParams<?> delete() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        Integer role = memberMapper.getRoleByMemberId(memberId);
        if (role == null || !role.equals(3)) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }


        Integer id = Integer.valueOf(request.getParameter("id"));
        if (StringUtils.isEmpty(id)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("id"));
        }

        Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().select("org_id").eq("member_id", memberId)).getOrgId();

        orgCarMapper.delete(new QueryWrapper<OrgCar>().eq("id", id).eq("org_id", orgId));

        return ResponseParams.ok(null);
    }


    public ResponseParams<?> addOrEdit() throws Exception {
        Integer memberId = ss.getCurrentUserId();
        String type = request.getParameter("type");
        String number = request.getParameter("number");
        String carWeight = request.getParameter("carWeight");
        String driverId = request.getParameter("driverId");
        if (StringUtils.isEmpty(type)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("type"));
        }
        if (StringUtils.isEmpty(number)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("number"));
        }
        if (StringUtils.isEmpty(carWeight)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("carWeight"));
        }

        Integer role = memberMapper.getRoleByMemberId(memberId);
        if (role == null || !role.equals(3)) {
            throw RequestException.create(ResponseParams.isDataError("无权限"));
        }

        String id = request.getParameter("id");
        Integer orgId = orgMemberMapper.selectOne(new QueryWrapper<OrgMember>().select("org_id").eq("member_id", memberId)).getOrgId();

        OrgCar orgCar = new OrgCar();
        orgCar.setCarWeight(carWeight);
        orgCar.setType(type);
        orgCar.setOrgId(orgId);
        orgCar.setNumber(number);
        if (!StringUtils.isEmpty(driverId)) {
            orgCar.setDriverId(Integer.parseInt(driverId));
            if (!ObjectUtils.isEmpty(orgCarMapper.selectOne(new QueryWrapper<OrgCar>().eq("driver_id", driverId)))) {
                throw RequestException.create(ResponseParams.isDataError("该司机已分配车辆，请分配其他司机，请刷新重试！"));
            }
        }
        if (StringUtils.isEmpty(id)) {
            Integer count = orgCarMapper.selectCount(new QueryWrapper<OrgCar>().eq("number", number));
            if (count != 0) {
                throw RequestException.create(ResponseParams.isDataError("该车牌号重复"));
            }
            orgCar.setCreateDate(new Date());
            orgCarMapper.insert(orgCar);
        } else {
            Integer count = orgCarMapper.selectCount(new QueryWrapper<OrgCar>().eq("number", number).ne("id",id));
            if (count != 0) {
                throw RequestException.create(ResponseParams.isDataError("该车牌号重复"));
            }
            orgCar.setId(Integer.valueOf(id));
            orgCar.setUpdateDate(new Date());
            orgCarMapper.update(orgCar, new QueryWrapper<OrgCar>().eq("org_id", orgId).eq("id", id));
        }

        return ResponseParams.ok(null);
    }


}
